/*
* pps_bom
*/
package com.pitop.assembly.ctrl;


import com.alibaba.fastjson.JSON;
import com.pitop.assembly.entity.AssemblyCategory;
import com.pitop.assembly.service.AssemblyCategorySVImpl;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.enums.YNEnum;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.StringTools;
import com.pitop.core.tools.redis.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * 组件类型 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/assemblyCategory")
@Slf4j
@Api(value = "组件类型控制器", description = "组件类型控制器")
public class AssemblyCategoryCtrl {

    @Resource
    protected RedisUtils redisUtils;
    @Autowired
    private AssemblyCategorySVImpl assemblyCategorySVImpl;


    /**
     * 查询AssemblyCategory一个详情信息
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询AssemblyCategory一个详情信息", notes = "查询AssemblyCategory一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        AssemblyCategory assemblyCategory = assemblyCategorySVImpl.load(id, code);
        log.info(JSON.toJSONString(assemblyCategory));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assemblyCategory);
    }

    /**
     * 查询AssemblyCategory一个详情信息
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询AssemblyCategory一个详情信息", notes = "查询AssemblyCategory一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/get")
    @ResponseBody
    public BeanRet get(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        AssemblyCategory assemblyCategory = assemblyCategorySVImpl.get(id, code);
        log.info(JSON.toJSONString(assemblyCategory));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assemblyCategory);
    }


    /**
     * 根据条件id查询AssemblyCategory一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询AssemblyCategory一个详情信息", notes = "查询AssemblyCategory一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public BeanRet loadById(@PathVariable Long id) {
        if (id == null || id == 0) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        AssemblyCategory assemblyCategory = assemblyCategorySVImpl.loadById(id);
        log.info(JSON.toJSONString(assemblyCategory));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assemblyCategory);
    }

    /**
     * 根据条件code查询AssemblyCategory一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询AssemblyCategory一个详情信息", notes = "查询AssemblyCategory一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public BeanRet loadByCode(@PathVariable String code) {
        if (StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        AssemblyCategory assemblyCategory = assemblyCategorySVImpl.loadByCode(code);
        log.info(JSON.toJSONString(assemblyCategory));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assemblyCategory);
    }

    /**
     * 根据条件id查询AssemblyCategory一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询AssemblyCategory一个详情信息", notes = "查询AssemblyCategory一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/get/id/{id}")
    @ResponseBody
    public BeanRet getById(@PathVariable Long id) {
        if (id == null || id == 0) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        AssemblyCategory assemblyCategory = assemblyCategorySVImpl.getById(id);
        log.info(JSON.toJSONString(assemblyCategory));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assemblyCategory);
    }

    /**
     * 根据条件code查询AssemblyCategory一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询AssemblyCategory一个详情信息", notes = "查询AssemblyCategory一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/get/code/{code}")
    @ResponseBody
    public BeanRet getByCode(@PathVariable String code) {
        if (StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        AssemblyCategory assemblyCategory = assemblyCategorySVImpl.getByCode(code);
        log.info(JSON.toJSONString(assemblyCategory));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assemblyCategory);
    }

    /**
     * 查询AssemblyCategory信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询AssemblyCategory信息集合", notes = "查询AssemblyCategory信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "preCode", value = "上级编码", paramType = "query"),
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "isShowAll", value = "是否显示全部数据", paramType = "query"),

    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore AssemblyCategory assemblyCategory,String isShowAll, @ApiIgnore Page<AssemblyCategory> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<AssemblyCategory> assemblyCategorys = assemblyCategorySVImpl.list(assemblyCategory,isShowAll, page.getCurPage(), page.getPageSize());
        if(StringTools.isEmpty(isShowAll)) {
            isShowAll = YNEnum.N.name();
        }
        if(YNEnum.N == YNEnum.getYN(isShowAll)) {
            int total = assemblyCategorySVImpl.count(assemblyCategory);
            page.setTotalRow(total);
        }
        page.setVoList(assemblyCategorys);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }



    /**
     * 查询AssemblyCategory信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询AssemblyCategory信息集合", notes = "查询AssemblyCategory信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public BeanRet listByPk(Long id, String code, @ApiIgnore Page<AssemblyCategory> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<AssemblyCategory> assemblyCategorys = assemblyCategorySVImpl.list(id, code, page.getCurPage(), page.getPageSize());
        int total = assemblyCategorySVImpl.count(id, code);
        page.setTotalRow(total);
        page.setVoList(assemblyCategorys);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }

    /**
     * 创建AssemblyCategory
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建AssemblyCategory", notes = "创建AssemblyCategory")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "preCode", value = "上级编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "类型名", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "描述", paramType = "query"),
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@ApiIgnore AssemblyCategory assemblyCategory) {
        if (StringUtils.isEmpty(assemblyCategory.getName())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        assemblyCategorySVImpl.save(assemblyCategory);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assemblyCategory);
    }

    /**
     * 修改AssemblyCategory
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改AssemblyCategory", notes = "修改AssemblyCategory")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "preCode", value = "上级编码", paramType = "query"),
            @ApiImplicitParam(name = "level", value = "上级级别", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "类型名", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "描述", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态[启用 enable,禁用 disable]", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore AssemblyCategory assemblyCategory) {
        if (StringUtils.isEmpty(assemblyCategory.getCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(assemblyCategory.getName())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        assemblyCategorySVImpl.modify(assemblyCategory);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assemblyCategory);
    }

    /**
     * 修改状态
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改AssemblyCategory", notes = "修改AssemblyCategory")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query", required = true),
            @ApiImplicitParam(name = "state", value = "状态[启用 Enable,禁用 Disable]", paramType = "query", required = true)
    })
    @PutMapping("/updateState")
    @ResponseBody
    public BeanRet updateState(String code, String state) {
        if (StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(state)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        assemblyCategorySVImpl.updateState(code, state);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success);
    }

    /**
     * 根据主键删除AssemblyCategory
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除AssemblyCategory", notes = "删除AssemblyCategory")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id, String code) {
        if (id != null && StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }

        assemblyCategorySVImpl.delete(id, code);
        return BeanRet.create(true, "删除AssemblyCategory成功");
    }

}
